Time Series

Module 11

Ray J. Hoobler

Libraries

Code
library(tidyverse)
library(lubridate)
library(fpp3) # 

Resource for Time Series Models

Definition of Time Series

An ordered sequence of values of a variable at equally spaced time intervals.

From fpp3:

Examples of time series data include:

Annual Google profits
Quarterly sales results for Amazon
Monthly rainfall
Weekly retail sales
Daily IBM stock prices
Hourly electricity demand
5-minute freeway traffic counts
Time-stamped stock transaction data

Anything that is observed sequentially over time is a time series.

Moving Averages (1/)

Code
supplier <- read_table(
  "Supplier     Cost    Error   ErrorSquared
1   9   -1  1
2   8   -2  4
3   9   -1  1
4   12  2   4
5   9   -1  1
6   12  2   4
7   11  1   1
8   7   -3  9
9   13  3   9
10  9   -1  1
11  11  1   1
12  10  0   0 ", col_names = TRUE
)

supplier

The mean cost is: 10.
The SSE is: 36.
The MSE is: 3.

Moving Averages (2/)

Code
pc_ebt <- read_table(
  "Year     Earnings_M  Mean    Error   Squared_Error
1985    46.163  48.676  -2.513  6.313
1986    46.998  48.676  -1.678  2.814
1987    47.816  48.676  -0.860  0.739
1988    48.311  48.676  -0.365  0.133
1989    48.758  48.676  0.082   0.007
1990    49.164  48.676  0.488   0.239
1991    49.548  48.676  0.872   0.761
1992    48.915  48.676  0.239   0.057
1993    50.315  48.676  1.639   2.688
1994    50.768  48.676  2.092   4.378", col_names = TRUE
)

pc_ebt

The mean Earnings ($M) is: 48.68.
The SSE is: 18.129.
The MSE is: 1.81.

In Practice

Real World Example: SEMI Forecasting

Global Silicon Wafer Shipments

Units are in “million square inches (MSI)”

Code
msi_historical <- read_tsv("datasets/MSI.txt", col_names = c("year", "Q1", "Q2", "Q3", "Q4"))
msi_historical

Wrangle the Data into a Long Format (1/)

year quarter msi
2001 Q1 1250
2001 Q2 988

And convert to a time series object (tsibble)

Code
msi_ts <- msi_historical |> 
  pivot_longer(cols = -year, names_to = "quarter", values_to = "quarter_value", ) |> 
  separate_wider_delim(cols = quarter_value, names = c("quarter_2", "msi"), delim = " ") |> 
  mutate(msi = as.numeric(str_remove(msi, ","))) |> 
  select(-quarter_2) |> 
  mutate(year_quarter = str_c(year, " ", quarter)) |> 
  select(year_quarter, msi) |> 
  mutate(year_quarter = yearquarter(year_quarter)) |> 
  as_tsibble(index = year_quarter)

msi_ts

Plot the Time Series

Code
msi_ts |> 
  autoplot(msi)

Code
msi_ts |> 
  gg_season(msi)

Code
msi_ts |> 
  gg_lag(msi, geom = "point")

Code
msi_ts |> 
  ACF(msi) |> 
  autoplot()